 var app;
 
 $(document).ready(function() {
	
	app = new ArabicSrtFix(); 
	 
	//-------------------------------------------------------------------
	//
	// load additional script files
	//
	//------------------------------------------------------------------- 
	jQuery.getScript( path() + 'js/TimecodeUtils.js'); 
	jQuery.getScript( path() + 'js/DropTarget.js', function() {
		new DropTarget( $('.step1 textarea')[0] );
	})

	//-------------------------------------------------------------------
	//
	// init UI
	//
	//-------------------------------------------------------------------
	
	$('.btn-loadDefaults').click(function(e) {
		e.preventDefault();
		app.loadDefaults();
	}); 
	
	$('.step1 .proceed').click(function(e) {
		e.preventDefault();
		app.run();
	}); 
	
	$('.step2 .btn-back').click(function(e) {
		e.preventDefault();
		app.back();
	});  
	 
	$('.step2').hide();
  
	setTimeout(app.onresize, 100);
	$(window).resize(app.onresize);  
	
	
});

var ArabicSrtFix = function() { 
}
ArabicSrtFix.prototype = {
		
	loadDefaults: function() {  
		$('.input textarea').load( path() + 'apps/arab-srt/problema_ger_1803_Arb_utf8.srt' );
		return '...loading default file.';
	},
	
	onresize: function() {
		$('table').each(function(i,table) {
			table = $(table);
			var table_height = $(window).height() - table.position().top;			 
			table.height( table_height );			
			table.find('.panel').height(  table_height - 20  )
			table.find('textarea').height(  table_height - 100  )
		}); 
	}, 
	
	back: function() {
		$('.step2').hide();	
		$('.step1').show();
		setTimeout(this.onresize, 100);
	},
	
	run: function() {  
		 
		var input,
			input_lines,
			result;
		
		//-----------------------------------------------------------------------------------------------
		//
		// INPUT VALIDATION
		//
		//-----------------------------------------------------------------------------------------------
		
		input = $('.input textarea').val();
		if (!input) {
			return alert('Please specify input!');
		};
		  

		//-----------------------------------------------------------------------------------------------
		//
		// parse lines
		//
		//-----------------------------------------------------------------------------------------------
		  
		result = [];	
		$(input.split('\n')).each(function(i,line){ 
			if (line.substr(0,5)=='0000 ') {
				return;
			}
			var timecodes = TimecodeUtils.edl.get(line);
			if (timecodes) {
				var ln_num = parseInt(line.split(' : ')[0], 10);
				result.push(ln_num);
				result.push(TimecodeUtils.srt.tcFrom(timecodes[0])+' --> '+TimecodeUtils.srt.tcFrom(timecodes[1]));
			}
			else {
				result.push(line);
			}
		});
		if (result[0]=='') {
			result.shift();
		}

		//-----------------------------------------------------------------------------------------------
		//
		// update the UI
		//
		//-----------------------------------------------------------------------------------------------

		// display results
		$('.input textarea').val(input);
		$('.output textarea').val(result.join('\n'));
		
		// show step2, hide step1
		$('.step1').hide();
		$('.step2').show();
		setTimeout(app.onresize, 100);
		 
		// reset scroll position
		$('.step2.input textarea').scrollTop(0);
		$('.step2.output textarea').scrollTop(0); 
		
		// synchronize scrolling of textareas
		$('.step2.input textarea').scroll(function() {
			$('.output.step2 textarea').scrollTop( $('.input.step2 textarea').scrollTop() );
		}); 
		$('.step2.output textarea').scroll(function() {
			$('.input.step2 textarea').scrollTop( $('.output.step2 textarea').scrollTop() );
		});
	}
};
